Skip to content

Fixes #27990: stop malformed search input failing as a server error (backport 2.0 #33655) - #35440

Merged
mohityadav766 merged 1 commit into
2.0from
fix/27990-malformed-search-400-2.0
Oct 14, 2026
Merged

mohityadav766 merged 1 commit into
2.0from
fix/27990-malformed-search-400-2.0

Conversation

@manerow

@manerow manerow commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Issue

Cherry-pick of PR #33655 (Fixes #27990)

On 2.0.x, a search whose text Lucene cannot parse, such as a glossary term name with a / and a parenthesis, fails with a 500 and lands in error tracking, although nothing on the server went wrong. Main has handled this since PR #33655. This backport brings the same behaviour to 2.0, so it ships in 2.0.6. It is also what #31878 asks for.

What this PR changes

The same three changes as on main (see PR #33655 for the full explanation):

  1. Text that Lucene cannot parse is no longer sent as a query_string. It is searched as plain words, so the caller gets results instead of an error.
  2. A queryFilter that is not usable as query DSL is rejected with a 400 that names queryFilter, instead of failing inside the search engine.
  3. A 400 from the search engine is reported as a 400, not a 500. Every other engine error stays a 500.

One adaptation was needed. On main, the fuzzy part of the query already falls back to simple_query_string for free-text endpoints (PR #31142), and the fix sends unparseable text down that same path. 2.0 does not have that change, so this PR copies its simpleQueryStringQuery helper, unchanged, into the 2.0 OpenSearchQueryBuilder and ElasticQueryBuilder, and the fuzzy branch uses it whenever the text is not well-formed Lucene. Without it, that text would still reach query_string and fail, only as a 400 instead of returning results.

The cherry-pick conflicted in imports of both search managers (both sides kept), in the two source builder factories (the adaptation above), and in SearchResourceIT. There, 2.0 already has the stricter negative-offset test and the httpGetSearch helper from PR #35302, so both were kept and the PR's identical copy of the helper was dropped, along with two imports that only main's other tests use. Every other file applies main's change line for line.

One behaviour change for API callers: a queryFilter that is not valid JSON used to be logged and silently ignored, so the search ran without it and returned more than asked. It is now a 400, as on main since 22 September.

How it was tested

On this branch:

  • The search unit tests (LuceneQuerySyntaxTest, QueryFilterShapeTest, SearchEngineErrorsTest, SearchSourceBuilderFactoryTest, FuzzySearchClauseTest and 12 other search classes): 297 tests, all passing.
  • The PR's SearchResourceIT tests against Postgres and OpenSearch: 12 tests, all passing. For that local run I also added the query from the production report, Uncategorized / Unknown (activity), as an extra case; it returns 200. That case is not part of this PR, to keep the tests identical to main.

@manerow
manerow requested a review from a team as a code owner October 14, 2026 11:07
@manerow manerow added safe to test Add this label to run secure Github workflows on PRs backend labels Oct 14, 2026
@github-actions

Copy link
Copy Markdown
Contributor

❌ PR checklist incomplete

This PR cannot be merged until the following are addressed on its linked issue:

  • No GitHub issue is linked. Link an issue in the Development section of the PR (or add Fixes #12345 to the description). For a same-org cross-repo issue, add Fixes open-metadata/<repo>#123 to the description.

The fields live on the linked issue in the Shipping project (open the issue → right sidebar → Projects). After you set them, re-run this check (or push a commit) — issue/project changes do not re-trigger it automatically.

Maintainers can bypass this check by adding the skip-pr-checks label.

@manerow manerow added the skip-pr-checks Bypass PR metadata validation check label Oct 14, 2026
Comment on lines +391 to +395
String queryDsl = QueryFilterShape.requireQueryDsl(queryFilter);
Query filterQuery = Query.of(q -> q.withJson(new StringReader(queryDsl)));
Query existingQuery = requestBuilder.query();
requestBuilder.query(
existingQuery == null ? filterQuery : filteredBy(existingQuery, filterQuery));

@gitar-bot gitar-bot Bot Oct 14, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Edge Case: Elasticsearch: DSL the client cannot parse now fails as a 500

On Elasticsearch, the filter is parsed on the client with Query.withJson(new StringReader(queryDsl)). QueryFilterShape only checks the bool clause shapes. So a filter such as {"query":{"not_a_query":{}}} gets past the check, and then withJson throws a JsonpMappingException (a RuntimeException) while the request is being built. The old catch (Exception) that swallowed this was removed and nothing replaced it, so the caller gets a 500 instead of the 400 this PR promises. OpenSearch sends the filter inside a wrapper query, so the engine rejects it and the PR's mapping turns that into a 400. Fix: catch the client parse failure in the ES applyQueryFilter and rethrow it as SearchException.withStatus(BAD_REQUEST, ...) with a message naming queryFilter. Add a test for an unknown query type.

Report a filter the client cannot parse as a 400:

String queryDsl = QueryFilterShape.requireQueryDsl(queryFilter);
Query filterQuery;
try {
  filterQuery = Query.of(q -> q.withJson(new StringReader(queryDsl)));
} catch (JsonpMappingException | JsonParsingException e) {
  throw SearchException.withStatus(
      Response.Status.BAD_REQUEST, "queryFilter is not a valid query: " + e.getMessage());
}

Was this helpful? React with 👍 / 👎

@gitar-bot

gitar-bot Bot commented Oct 14, 2026 •

Copy link
Copy Markdown
Code Review 👍 Approved with suggestions 0 closed / 1 findings

🟡 Medium risk · Routing malformed Lucene searches to literal matching and rejecting invalid queryFilter as 400 could change search results and client error handling.

Stops malformed search input (for example, Lucene-unparseable text like a glossary term with / and parentheses) from failing as a 500: such text is now searched as plain words, unusable queryFilter values return a 400 naming queryFilter, and engine 400s are reported as 400s. Approved with suggestions.

  • Elasticsearch: a queryFilter the client cannot parse (for example, an unknown query type like {"query":{"not_a_query":{}}}) still fails as a 500 in ElasticSearchSearchManager.applyQueryFilter. Consider catching the client parse failure there and rethrowing it as a 400 that names queryFilter, with a test for an unknown query type.
💡 Edge Case: Elasticsearch: DSL the client cannot parse now fails as a 500

📄 openmetadata-service/src/main/java/org/openmetadata/service/search/elasticsearch/ElasticSearchSearchManager.java:391-395 📄 openmetadata-service/src/main/java/org/openmetadata/service/search/QueryFilterShape.java:42-49

On Elasticsearch, the filter is parsed on the client with Query.withJson(new StringReader(queryDsl)). QueryFilterShape only checks the bool clause shapes. So a filter such as {"query":{"not_a_query":{}}} gets past the check, and then withJson throws a JsonpMappingException (a RuntimeException) while the request is being built. The old catch (Exception) that swallowed this was removed and nothing replaced it, so the caller gets a 500 instead of the 400 this PR promises. OpenSearch sends the filter inside a wrapper query, so the engine rejects it and the PR's mapping turns that into a 400. Fix: catch the client parse failure in the ES applyQueryFilter and rethrow it as SearchException.withStatus(BAD_REQUEST, ...) with a message naming queryFilter. Add a test for an unknown query type.

Report a filter the client cannot parse as a 400
String queryDsl = QueryFilterShape.requireQueryDsl(queryFilter);
Query filterQuery;
try {
  filterQuery = Query.of(q -> q.withJson(new StringReader(queryDsl)));
} catch (JsonpMappingException | JsonParsingException e) {
  throw SearchException.withStatus(
      Response.Status.BAD_REQUEST, "queryFilter is not a valid query: " + e.getMessage());
}
🤖 Prompt for agents
Code Review: Stops malformed search input (for example, Lucene-unparseable text like a glossary term with `/` and parentheses) from failing as a 500: such text is now searched as plain words, unusable `queryFilter` values return a 400 naming `queryFilter`, and engine 400s are reported as 400s. Approved with suggestions.
  
  - Elasticsearch: a `queryFilter` the client cannot parse (for example, an unknown query type like `{"query":{"not_a_query":{}}}`) still fails as a 500 in `ElasticSearchSearchManager.applyQueryFilter`. Consider catching the client parse failure there and rethrowing it as a 400 that names `queryFilter`, with a test for an unknown query type.

1. 💡 Edge Case: Elasticsearch: DSL the client cannot parse now fails as a 500
   Files: openmetadata-service/src/main/java/org/openmetadata/service/search/elasticsearch/ElasticSearchSearchManager.java:391-395, openmetadata-service/src/main/java/org/openmetadata/service/search/QueryFilterShape.java:42-49

   On Elasticsearch, the filter is parsed on the client with `Query.withJson(new StringReader(queryDsl))`. `QueryFilterShape` only checks the bool clause shapes. So a filter such as `{"query":{"not_a_query":{}}}` gets past the check, and then `withJson` throws a `JsonpMappingException` (a `RuntimeException`) while the request is being built. The old `catch (Exception)` that swallowed this was removed and nothing replaced it, so the caller gets a 500 instead of the 400 this PR promises. OpenSearch sends the filter inside a `wrapper` query, so the engine rejects it and the PR's mapping turns that into a 400. Fix: catch the client parse failure in the ES `applyQueryFilter` and rethrow it as `SearchException.withStatus(BAD_REQUEST, ...)` with a message naming `queryFilter`. Add a test for an unknown query type.

   Fix (Report a filter the client cannot parse as a 400):
   String queryDsl = QueryFilterShape.requireQueryDsl(queryFilter);
   Query filterQuery;
   try {
     filterQuery = Query.of(q -> q.withJson(new StringReader(queryDsl)));
   } catch (JsonpMappingException | JsonParsingException e) {
     throw SearchException.withStatus(
         Response.Status.BAD_REQUEST, "queryFilter is not a valid query: " + e.getMessage());
   }

Verification

Rules No rules evaluated

Functional validation Not enabled

Set up

Auto-approval Not enabled

Set up

For more verification details, comment gitar display:verbose.

Options

Display: compact → Showing compact verification results.

Comment with these commands to change the behavior for this request:

Compact
gitar display:verbose         

Was this helpful? React with 👍 / 👎 | Powered by Gitar — free for open source

@sonarqubecloud

Copy link
Copy Markdown

@mohityadav766
mohityadav766 merged commit 5328420 into 2.0 Oct 14, 2026
134 of 149 checks passed
@mohityadav766
mohityadav766 deleted the fix/27990-malformed-search-400-2.0 branch October 14, 2026 14:27

This branch was previously deployed

1 inactive deployment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend safe to test Add this label to run secure Github workflows on PRs skip-pr-checks Bypass PR metadata validation check

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants